//
//  RankBookCell.swift
//  WalkNovel
//
//  Created by Kevin on 2021/11/4.
//

import UIKit

class RankBookCell: UITableViewCell {
    
    lazy var bookImage: UIImageView = {
        let image = UIImageView(image: UIImage(named: "placeholder_cover_ver"))
        image.contentMode = .scaleAspectFill
        image.wn_cornerWithBorder(6)
        image.clipsToBounds = true
        return image
    }()
    
    lazy var numImage: UIImageView = {
        return UIImageView()
    }()

    lazy var numLabel: UILabel = {
        let label = UILabel()
        label.font = .wn_font(name: .regular, size: 8)
        label.textColor = .white
        label.textAlignment = .center
        label.backgroundColor = .init(named: "red_FE95B4")
        return label
    }()
    
    lazy var nameLabel: UILabel = {
        let label = UILabel()
        label.font = .wn_font(name: .bold, size: 14)
        label.numberOfLines = 0
        label.textColor = .black
        return label
    }()

    lazy var desLabel: UILabel = {
        let label = UILabel()
        label.font = .wn_font(name: .bold, size: 12)
        label.textColor = .init(named: "gray_999999")
        label.numberOfLines = 2
        return label
    }()
    
    lazy var countBtn: UIButton = {
        let btn = UIButton(type: .custom)
        btn.setTitleColor(.black, for: .normal)
        btn.titleLabel?.font = .wn_font(name: .bold, size: 10)
        btn.setImage(UIImage(named: "discover_hot"), for: .normal)
        btn.imageView?.contentMode = .scaleAspectFit
        return btn
    }()

    var model: BookDetailModel? {
        didSet {
            if let m = model {
                bookImage.wn_setupImg(model?.coverUrl, placeHolder: "placeholder_cover_ver")
                nameLabel.text = m.name
                desLabel.text = m.des
                countBtn.setTitle("\(m.num)".wn_wan(), for: .normal)
            }
        }
    }
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        selectionStyle = .none
        backgroundColor = .white
        contentView.addSubview(bookImage)
        bookImage.addSubview(numImage)
        bookImage.addSubview(numLabel)
        contentView.addSubview(nameLabel)
        contentView.addSubview(desLabel)
        contentView.addSubview(countBtn)

        bookImage.snp.makeConstraints { (make) in
            make.centerY.equalToSuperview()
            make.left.equalTo(12)
            make.size.equalTo(CGSize(width: scaleWidth(61), height: scaleWidth(90)))
        }
        numImage.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
        numLabel.snp.makeConstraints { make in
            make.top.left.equalToSuperview()
            make.width.height.equalTo(17)
        }
        nameLabel.snp.makeConstraints { (make) in
            make.left.equalTo(bookImage.snp.right).offset(6)
            make.top.equalTo(bookImage.snp.top).offset(0)
            make.right.equalTo(-22)
        }
        desLabel.snp.makeConstraints { (make) in
            make.left.equalTo(nameLabel.snp.left)
            make.top.equalTo(nameLabel.snp.bottom).offset(12)
            make.right.equalTo(-46)
        }
        countBtn.snp.makeConstraints { (make) in
            make.centerY.equalToSuperview()
            make.right.equalTo(-12)
            make.height.equalTo(10)
        }
        numLabel.wn_cornerWithBorder(2)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

